package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dao.ShoppingCartDao;
import com.itheima.reggie.domain.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;


    @Override
    public List<ShoppingCart> findShopping() {

        //根据当前登录用户id查询购物车信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(lqw);

        return shoppingCarts;
    }


    //添加购物车
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //获取当前登录用户id
        Long shopId = BaseContext.getThreadLocal();
        shoppingCart.setUserId(shopId);

        //获取对应的dishId
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //select * from shopping_cart where userId= ? and dish_id/setmeal_id = ?

        lqw.eq(ShoppingCart::getUserId,shopId);

        //判断是添加菜品还是套餐
        lqw.eq(dishId!=null,ShoppingCart::getDishId,dishId);
        lqw.eq(setmealId!=null,ShoppingCart::getSetmealId,setmealId);

        //判断是否有数据
        ShoppingCart selectOne = shoppingCartDao.selectOne(lqw);
        if (selectOne!=null){
            //说明数据库里有这条数据,使其number字段加一
            Integer number = selectOne.getNumber();
            selectOne.setNumber(number+1);
            shoppingCartDao.updateById(selectOne);
        }else{
            //说明数据库里没有这条数据
            //设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartDao.insert(shoppingCart);
            selectOne=shoppingCart;
        }

        return selectOne;
    }

    @Override
    public void deleteByUserId() {
        //根据当前登录用户id清空购物车
        Long userId = BaseContext.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartDao.delete(lqw);
    }


    //购物车按钮数量减一
    @Override
    public ShoppingCart MinusOne(ShoppingCart shoppingCart) {
        //获取当前用户id
        Long userId = BaseContext.getThreadLocal();

        //获取dishId或者setmealId
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //根据dishId或setmealId查询购物车对应的菜品或套餐信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //select * from shopping_cart where userId = ? and dishId = ?或者 setmealId = ?
        lqw.eq(dishId !=null,ShoppingCart::getDishId,dishId);
        lqw.eq(ShoppingCart::getUserId,userId);
        lqw.eq(setmealId !=null,ShoppingCart::getSetmealId,setmealId);

        ShoppingCart shoppingCart1 = shoppingCartDao.selectOne(lqw);
            Integer number = shoppingCart1.getNumber();
            //判断其中的number是否等于1
            if (number==1){
                //等于1 则将这个套餐或菜品从数据库中删除,并清空购物车
                //delete from shopping_cart where userId = ? and dishId = ?或者 setmealId = ?
                shoppingCart1.setNumber(number-1);
                shoppingCartDao.delete(lqw);

            }else{
                //大于1 number-1
                shoppingCart1.setNumber(number-1);
                shoppingCartDao.updateById(shoppingCart1);

            }


        return shoppingCart1;

    }


}
